package udpSve;

import java.util.ArrayList;
import java.util.List;

public class WorkerProstiBrojevi extends Thread {

	private volatile int start, end, chunkId;
	private volatile boolean working, running;
	private List<Integer> prosti;

	public WorkerProstiBrojevi() {
		chunkId = -1;
		start = 0;
		end = 0;
		working = false;
		prosti = new ArrayList<>();

	}

	@Override
	public void run() {
		running = true;
		while (running) {
			while ((start == 0 || end == 0) && running) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (running) {
				System.out.println("WorkerProstiBrojevi: Pocinjem da radim: "
						+ start + " " + end);
				working = true;
				for (int i = start; i <= end; i++) {
					if (prost(i)) {
						prosti.add(i);
					}
				}
				System.out.println("WorkerProstiBrojevi: Prestao da radim: "
						+ start + " " + end);
				start = 0;
				end = 0;
				working = false;
			}

		}
	}

	private boolean prost(int i) {
		for (int j = 2; j < i; j++) {
			if (i % j == 0)
				return false;
		}
		return true;
	}

	public List<Integer> getProsti() {
		return prosti;
	}

	public void setProsti(List<Integer> prosti) {
		this.prosti = prosti;
	}

	public boolean isWorking() {
		return working;
	}

	// public void setStart(int start) {
	// working=true;
	// this.start = start;
	// }
	// public void setEnd(int end) {
	// working=true;
	// this.end = end;
	// }
	public void setChunk(int chunkId, int start, int end) {
		working = true;
		this.chunkId = chunkId;
		this.start = start;
		this.end = end;
	}

	public void setRunning(boolean running) {
		this.running = running;
	}

	public boolean isRunning() {
		return running;
	}

	public int getChunkId() {
		return chunkId;
	}

	public void setChunkId(int chunkId) {
		this.chunkId = chunkId;
	}

	public int getStart() {
		return start;
	}

	public int getEnd() {
		return end;
	}

}
